/*
 * @Author: gggg 1263457581@qq.com
 * @Date: 2025-06-06 18:20:25
 * @LastEditors: gggg 1263457581@qq.com
 * @LastEditTime: 2025-06-06 18:21:56
 * @FilePath: /algorithm-and-data-struct/algorithm/string/InvertTable.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

void BBEncMvList::canmv2blkmvs(std::map<mv, int, MvCmp_Y>& can_mv) {
    std::map<int, std::vector<mv> > invert_table;
    int biggest_count = -1;
    int secondbig_count = -1;
    for (auto it = can_mv.begin(); it != can_mv.end(); it++)
    {
        std::pair<mv, int> tmp = *it;
        if (tmp.second > biggest_count) {
            secondbig_count = biggest_count;
            biggest_count = tmp.second;
        }
        else if (tmp.second > secondbig_count) {
            secondbig_count = tmp.second;
        }
        if (invert_table.find(tmp.second) == invert_table.end()) {
            std::vector<mv> new_vec;
            invert_table[tmp.second] = new_vec;
        }
        std::vector<mv>& tmp_vec = invert_table[tmp.second];
        tmp_vec.push_back(tmp.first);
    }
    if (biggest_count != -1) {
        std::vector<mv>& biggest_vec = invert_table[biggest_count];
        /*最多只保留2个合适的mv */
        for (size_t i = 0; i < (10 > biggest_vec.size() ? biggest_vec.size() : 10); i++)
        {
            pre_blk_mvs.push_front(biggest_vec[i]);
            #ifdef MOTION_VECTOR_PRINTF
            log_status("Display_channel_id[%d]_%d", display_channel_id, biggest_vec[i].y);
            #endif
        }
    }
    if (invert_table[biggest_count].size() == 1 && secondbig_count != -1) {
        if (invert_table.find(secondbig_count) != invert_table.end()) {
            pre_blk_mvs.push_front(invert_table[secondbig_count][0]);
            #ifdef MOTION_VECTOR_PRINTF
            log_status("Display_channel_id[%d]_%d", display_channel_id, invert_table[secondbig_count][0].y);
            #endif
        }
    }
}

void BBEncMvList::canmv2blkmvs(std::map<mv, int, MvCmp_X>& can_mv) {
    std::map<int, std::vector<mv> > invert_table;
    int biggest_count = -1;
    int secondbig_count = -1;
    for (auto it = can_mv.begin(); it != can_mv.end(); it++)
    {
        std::pair<mv, int> tmp = *it;
        if (tmp.second > biggest_count) {
            secondbig_count = biggest_count;
            biggest_count = tmp.second;
        }
        else if (tmp.second > secondbig_count) {
            secondbig_count = tmp.second;
        }
        if (invert_table.find(tmp.second) == invert_table.end()) {
            std::vector<mv> new_vec;
            invert_table[tmp.second] = new_vec;
        }
        std::vector<mv>& tmp_vec = invert_table[tmp.second];
        tmp_vec.push_back(tmp.first);
    }
    if (biggest_count != -1) {
        std::vector<mv>& biggest_vec = invert_table[biggest_count];
        /*最多只保留2个合适的mv */
        for (size_t i = 0; i < (2 > biggest_vec.size() ? biggest_vec.size() : 2); i++)
        {
            pre_blk_mvs.push_front(biggest_vec[i]);
            #ifdef MOTION_VECTOR_PRINTF
            log_status("Display_channel_id[%d]_%d", display_channel_id, biggest_vec[i].y);
            #endif
        }
    }
    if (invert_table[biggest_count].size() == 1 && secondbig_count != -1) {
        if (invert_table.find(secondbig_count) != invert_table.end()) {
            pre_blk_mvs.push_front(invert_table[secondbig_count][0]);
            #ifdef MOTION_VECTOR_PRINTF
            log_status("Display_channel_id[%d]_%d", display_channel_id, invert_table[secondbig_count][0].y);
            #endif
        }
    }
    
}